/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TareasPanel.java
 *
 * Created on 30-abr-2011, 16:58:37
 */

package ia_psr.transportesGUI;

/**
 *
 * @author Rubén Avalos Elvira y Fabiana Celiberti Tapie
 */
import javax.swing.JFrame;
import ia_psr.graph.Nodo;
import ia_psr.graph.Tarea;
import ia_psr.graph.Grafo;
import ia_psr.graph.GrafoEmpresaTransportes;
import ia_psr.builder.GraphBuilder;
import ia_psr.builder.GrafoEmpresaTransportesBuilder;
import ia_psr.problemas.Variable;
import ia_psr.problemas.transportes.Conductor;
import ia_psr.problemas.transportes.EmpresaTransportes;

public class TareasPanel extends javax.swing.JPanel {

    /** Creates new form TareasPanel */
    public TareasPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tareasTable = new javax.swing.JTable();
        btnAddTarea = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        bntPrintGrafo = new javax.swing.JButton();
        btnPrintConductores = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tareas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(102, 102, 102))); // NOI18N

        tareasTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nombre", "Tipo", "Hora Inicio", "Hora Fin", "Duración"
            }
        ));
        jScrollPane1.setViewportView(tareasTable);

        btnAddTarea.setText("Añadir");
        btnAddTarea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnAddTareaMouseReleased(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnEliminarMouseReleased(evt);
            }
        });

        bntPrintGrafo.setText("PrintGrafo");
        bntPrintGrafo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                bntPrintGrafoMouseReleased(evt);
            }
        });

        btnPrintConductores.setText("PrintConductores");
        btnPrintConductores.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnPrintConductoresMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bntPrintGrafo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPrintConductores)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 209, Short.MAX_VALUE)
                        .addComponent(btnAddTarea)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminar)
                    .addComponent(btnAddTarea)
                    .addComponent(bntPrintGrafo)
                    .addComponent(btnPrintConductores))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddTareaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddTareaMouseReleased
        showAddTareaDialog();
    }//GEN-LAST:event_btnAddTareaMouseReleased

    public void showAddTareaDialog() {
        JFrame frame = new AddTareaDialog(this);
        frame.setVisible(true);
    }

    private void btnEliminarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEliminarMouseReleased
        removeSelectedTarea();
    }//GEN-LAST:event_btnEliminarMouseReleased

    private void bntPrintGrafoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bntPrintGrafoMouseReleased
        Grafo g = GrafoEmpresaTransportesBuilder.getInstance().getBuildedGraph();
        GrafoEmpresaTransportes gt = (GrafoEmpresaTransportes)g;
        gt.print();
    }//GEN-LAST:event_bntPrintGrafoMouseReleased

    private void btnPrintConductoresMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPrintConductoresMouseReleased
        // TODO add your handling code here:
        EmpresaTransportes et = EmpresaTransportes.getInstance();
        System.out.println("");
        for (Variable v : et.elementos) {
            Conductor c = (Conductor)v;
            System.out.print(c.getValue() + ": ");
            for (Tarea t : c.tareasAsignadas) {
                System.out.print(t.nombre + ", ");
            }
            System.out.println("");
        }
    }//GEN-LAST:event_btnPrintConductoresMouseReleased

    public void removeSelectedTarea()
    {
        try {

            int i = tareasTable.getSelectedRow();
            Grafo g = GrafoEmpresaTransportesBuilder.getInstance().getBuildedGraph();
            Nodo n = g.getNodo(tareasTable.getModel().getValueAt(i, 0).toString());

            for (int j = 0; j < tareasTable.getColumnCount(); ++j) {
                tareasTable.setValueAt("", g.getNodos().size()-1, j);
            }
            g.removeNodo(n);
            populateTareasTable();

        } catch (Exception e) {

        }
    }

    public void clearTareasTable() {
        for (int i = 0; i < tareasTable.getRowCount(); ++i) {
            for (int j = 0; j < tareasTable.getColumnCount(); ++j) {
                tareasTable.setValueAt("", i, j);
            }
        }
    }

    public void populateTareasTable() {
        GraphBuilder b = GrafoEmpresaTransportesBuilder.getInstance();
        Grafo g = b.getBuildedGraph();

        for (int i = 0; i < g.getNodos().size(); ++i) {
            Tarea t = (Tarea)g.getNodos().get(i);
            tareasTable.getModel().setValueAt(t.nombre, i, 0);

            if (t.programada()) {
                tareasTable.getModel().setValueAt("Programada",   i, 1);
                tareasTable.getModel().setValueAt(t.h.horaInicio, i, 2);
                tareasTable.getModel().setValueAt(t.h.horaFin,    i, 3);
                tareasTable.getModel().setValueAt(t.duracion(),   i, 4);
            } else {
                tareasTable.getModel().setValueAt("No programada", i, 1);
                tareasTable.getModel().setValueAt("",              i, 2);
                tareasTable.getModel().setValueAt("",              i, 3);
                tareasTable.getModel().setValueAt(t.duracion(),    i, 4);
            }
        }
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntPrintGrafo;
    private javax.swing.JButton btnAddTarea;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnPrintConductores;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tareasTable;
    // End of variables declaration//GEN-END:variables

}
